#send data to pi through protocolbuffer rpc
import socket  
import struct
import rpcmsg_pb2
import time
import math

client = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )

#set socket timeout
client.settimeout(2.0) 

svr_addr = ('localhost', 2161) 

i=0.0
pts=[14045,14046,14196,14197,14198,14199,14047,14749,14049,14050]
try:
    while 1:   
        pi_points = rpcmsg_pb2.PIPoints()

        for pt in pts:
            pi_point = pi_points.pfs.add()
            pi_point.pt = pt
            pi_point.val = math.sin(i)*100
            pi_point.timestamp = 0
        
        print pi_points
        
        data_field = pi_points.SerializeToString()
        
        longs = [0,101,0,0,0,0,0,0]
        longs[7] = len(data_field)

        msg_head = struct.pack('8i',*longs)    
        
   
        send_rs = client.sendto( msg_head+data_field, svr_addr ) 
        print 'send', send_rs, 'bytes'
        
        i = (i+1.0/180.0)*10
        
        time.sleep(1)
    

        
except socket.error, msg:
    print msg
    
print 'end'